import 'package:flutter/material.dart';
import 'user_room.dart';
import 'check_in_room.dart';
import 'follow_room.dart';
import 'lake_recommend_room.dart';
import 'dynamic_room.dart';
import 'badge_room.dart';
import 'group_chat_room.dart';
import 'single_chat_room.dart';
import 'quiz_room.dart';
import 'ai_chat_room.dart';

/// 全局状态管理提供者
class GlobalStateProvider extends InheritedWidget {
  final UserRoom userRoom;
  final CheckInRoom checkInRoom;
  final FollowRoom followRoom;
  final LakeRecommendRoom lakeRecommendRoom;
  final DynamicRoom dynamicRoom;
  final BadgeRoom badgeRoom;
  final GroupChatRoom groupChatRoom;
  final SingleChatRoom singleChatRoom;
  final QuizRoom quizRoom;
  final AiChatRoom aiChatRoom;

  const GlobalStateProvider({
    super.key,
    required this.userRoom,
    required this.checkInRoom,
    required this.followRoom,
    required this.lakeRecommendRoom,
    required this.dynamicRoom,
    required this.badgeRoom,
    required this.groupChatRoom,
    required this.singleChatRoom,
    required this.quizRoom,
    required this.aiChatRoom,
    required super.child,
  });

  /// 获取用户状态管理实例
  static UserRoom userRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.userRoom;
  }

  /// 获取打卡状态管理实例
  static CheckInRoom checkInRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.checkInRoom;
  }

  /// 获取关注状态管理实例
  static FollowRoom followRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.followRoom;
  }

  /// 获取湖泊推荐状态管理实例
  static LakeRecommendRoom lakeRecommendRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.lakeRecommendRoom;
  }

  /// 获取动态状态管理实例
  static DynamicRoom dynamicRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.dynamicRoom;
  }

  /// 获取徽章状态管理实例
  static BadgeRoom badgeRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.badgeRoom;
  }

  /// 获取群聊状态管理实例
  static GroupChatRoom groupChatRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.groupChatRoom;
  }

  /// 获取单聊状态管理实例
  static SingleChatRoom singleChatRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.singleChatRoom;
  }

  /// 获取答题状态管理实例
  static QuizRoom quizRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.quizRoom;
  }

  /// 获取AI聊天状态管理实例
  static AiChatRoom aiChatRoomOf(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider.aiChatRoom;
  }

  /// 获取所有状态管理实例
  static GlobalStateProvider of(BuildContext context) {
    final provider =
        context.dependOnInheritedWidgetOfExactType<GlobalStateProvider>();
    if (provider == null) {
      throw Exception('GlobalStateProvider not found in widget tree');
    }
    return provider;
  }

  @override
  bool updateShouldNotify(GlobalStateProvider oldWidget) {
    return userRoom != oldWidget.userRoom ||
        checkInRoom != oldWidget.checkInRoom ||
        followRoom != oldWidget.followRoom ||
        lakeRecommendRoom != oldWidget.lakeRecommendRoom ||
        dynamicRoom != oldWidget.dynamicRoom ||
        badgeRoom != oldWidget.badgeRoom ||
        groupChatRoom != oldWidget.groupChatRoom ||
        singleChatRoom != oldWidget.singleChatRoom ||
        quizRoom != oldWidget.quizRoom ||
        aiChatRoom != oldWidget.aiChatRoom;
  }
}
